<script lang="ts" setup name="XtxCity">
import { ref, watch } from 'vue';
import {onClickOutside} from '@vueuse/core'
import axios  from 'axios';

// 选择的城市结果类型
export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}
// 接收父传过来的值
const {userAddress=''}= defineProps<{
  userAddress?:string
}>()
// 子向父传值
const emit=defineEmits<{
  // e事件名  value值
  (e:'changeCity',value:CityResult):void
}>()



//城市选择 弹窗
const flag=ref(false)
// 切换状态
const toggle=()=>{
  flag.value=!flag.value
}
// 点击弹窗外部 关弹窗
const target=ref(null)
onClickOutside(target,()=>{
  flag.value=false
})

// 获取省市区数据 城市数据在阿里云上 需要写原生axios
// 1.一次获取所有的数据(省市区)
// 2.先获取省 点击获取市，点击市获取县
// 3.用常量保存省市区数据
       // 城市列表类型
 type AreaList = {
        code: string
        level: number
        name: string
        areaList: AreaList[]
      }
      
const cityList=ref<AreaList[]>([])
const cacheCity=ref<AreaList[]>([]) //缓存数据
const getCityList= async()=>{
  const res=await axios.get('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  cityList.value=res.data
  cacheCity.value=res.data
}
getCityList()
// 选择城市 保存数据
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
// 点击省显示市 点击市显示区
const selectCity=(item:AreaList)=>{
    //  cityList.value=item.areaList
    // 省
    if(item.level===0){
      changeResult.value.provinceCode=item.code
      changeResult.value.provinceName=item.name
      cityList.value=item.areaList
    }
    // 市
    if(item.level===1){
      changeResult.value.cityCode=item.code
      changeResult.value.cityName=item.name
      cityList.value=item.areaList
    }
    // 区
    if(item.level===2){
      changeResult.value.countyCode=item.code
      changeResult.value.countyName=item.name
      // 关闭弹窗
      flag.value=false
      // 子向父
      emit('changeCity',changeResult.value)
    }
}
// 侦听flag变化
watch(flag,()=>{
  if(!flag.value){
    cityList.value=cacheCity.value
  }
})
</script>

<template>
  <div class="xtx-city" ref="target">
    <!-- 动态类名 默认false 点击取反 -->
    <div class="select" :class="{active:flag}" @click="toggle">
      <span class="value" v-if="userAddress">{{userAddress}}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!--  -->
    <div class="option" v-show="flag">
      <span class="ellipsis" v-for="item in cityList" :key="item.code" @click="selectCity(item)">{{item.name}}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
